/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * 返回反转后的链表的首个结点
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
  if (!head || !head.next) return head

  let pre = null, cur = head, nxt = head.next

  while (cur) {
    cur.next = pre
    pre = cur
    cur = nxt
    if (nxt) nxt = nxt.next
  }

  return pre
};